$(function() {
	// 编辑基础信息
	var isDisabled = $("#pCardType").prop("disabled");
	if (isDisabled) {
		var idType = $("#pCardType").val();

		var idText = $("#pCardType").find("option:selected").text();
		$("#certTypeHtml").remove();
		var idHtml = "<input type='hidden' name='pCardType' id='zhenjian' value=" + idType + " />";
		idHtml += "<span>" + idText + "</span>";
		$("#certType").append(idHtml);
	}

	$("#uploadifyButton2").uploadify({ // 上传小区鸟瞰图
		'swf' : "/IMS/js/plugins/file/uploadify/uploadify.swf",
		'uploader' : "/IMS/fileUpload.ht",
		'buttonText' : '',
		'fileTypeDesc' : '当前允许上传格式：*.jpg | *.gif | *.png',
		'fileTypeExts' : '*.gif; *.jpg; *.png',
		'queueID' : 'fileQueue2',
		'auto' : true,
		'multi' : false,
		'width' : 270,
		'height' : 200,
		'method' : 'post',
		'fileSizeLimit' : '10MB',
		'onUploadSuccess' : function(fileData, jsonData) {
			var obj;
			if (jsonData) {
				obj = eval('(' + jsonData + ')');
				if (obj.code == 1) {
					alert(obj.message);
				} else {
					if (obj.result) {
						$("#uploadifyButton2-button img").remove();
						$("#uploadifyButton2-button").append("<img src='" + obj.result[0] + "'/>");
						$("#tempFileName").val(obj.result[0]);
					}
				}
			}
		}
	});

	if (companyLog) {
		showUploadTip(fileServerUrl + companyLog, "uploadifyButton2");
	}
	$('#pSex').select2({
		placeholder : "请选择"
	});
	$('#cpSelect2').select2({
		placeholder : "请选择"
	});
	$('#pCardType').select2({
		placeholder : "请选择证件类型"
	});
})

function checkNav(obj) {
	var pid = $("#pid").val();
	if (pid || obj.id == 'a_base') {
		return true;
	} else {
		_alert("请填写基础信息并保存后再填写其他信息");
		return false;
	}
};

function saveBase() {
	if (chkNull('pName', '业主名称不能为空')) {
		return;
	} else {
		var chineseReg = /^[\u2E80-\u9FFF]+$/;
		if (checkReg(chineseReg, "业主名称必须是中文,其中不能包含特殊字符", $("#pName").val())) {
			return;
		}
	}
	var vale = $("input[name='selectHids']").val();

	if (vale == '' || vale == null || vale == undefined) {
		_alert('房产不能为空', '', true);
		return;
	}
	if (chkNull('pMobile', '手机号码不能为空')) {
		return;
	}
	if (checkReg(checkPhoneReg, '请输入正确的手机号', $("#pMobile").val())) {
		return;
	}

	var pCardType = $("#pCardType option:selected").val();
	if (addOrUpdateFlag != '' && addOrUpdateFlag != null) {
		pCardType = $("#zhenjian").val();
	}
	if (pCardType == null || pCardType == '' || pCardType == '请选择') {
		_alert("请选择证件类型");
		return;
	}

	if (chkNull("pCardNum", "身份证不能为空")) {
		return;
	}
	if (!Is_Null($("#pCardNum").val())) {
		// 按照证件类型验证证件是否正确
		if ("01" == pCardType) { // 身份证
			if (checkReg(checkIDCard, "身份证格式不正确", $("#pCardNum").val())) {
				return;
			}
		} else if ("02" == pCardType) { // 港澳居民来往内地通行证
			if (checkReg(checkReturnHomeNo, "港澳居民来往内地通行证格式不正确", $("#pCardNum").val())) {
				return;
			}
		}
	}
	var pid = $("#pid").val();
	$("#saveBase").attr("disabled", true);
	if (pid != null && pid != '' && pid != undefined) { // 更新页面
		var pCardNum = $("#pCardNum").val();
		var mobile = $("#pMobile").val();
		// 校验手机号是否发生了变化 如果变化的话 在按照新的添加验证
		$.ajax({
			url : "/IMS/owner/checkOwnerMobileByidNo.ht",
			data : {
				pCardNum : pCardNum,
				mobile : mobile,
				cid : cid,
				pid : pid
			},
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccesBar();
			},
			success : function(data) {
				if (data.messageCode == message_false_code) {
					_alert(data.message);
					$("#saveBase").attr("disabled", false);
					returnBack();
					return;
				} else {
					$.submit($('#basefrm'), {
						dataType : 'json',
						error : function() {
							hideProccesBar();
						},
						success : function(data, textStatus) {
							if (data.result.code == 0 || data.result.code == '0') {
								$('#pid').val(data.result.baseinfo.pid);
								$('#attachpid').val(data.result.baseinfo.pid);
								_alert("保存成功", true, true);
								returnBack();
							} else if (data.result.code == 1 || data.result.code == '1') {
								_alert(data.result.message);
							}
							hideProccessBar();
						}
					});
				}
			}
		});
	} else {
		// 新增页面 实名认证业主的身份证和姓名
		var pCardNum = $("#pCardNum").val();
		var mobile = $("#pMobile").val();
		var name = $("#pName").val();

		$.submit($('#basefrm'), {
			dataType : 'json',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccesBar();
			},
			success : function(data, textStatus) {
				if (data.result.code == 0 || data.result.code == '0') {
					$('#pid').val(data.result.baseinfo.pid);
					$('#attachpid').val(data.result.baseinfo.pid);
					_alert("保存成功", true, true);
					returnBack();
				} else if (data.result.code == 1 || data.result.code == '1') {
					_alert(data.result.message);
				}
				hideProccessBar();
			}
		});
	}
}

function returnBack() {
	var url = "/IMS/owner/showList.ht";
	$.go(url, {
		dataType : 'html',
		data : {
			communityId : cid,
			pcid : pcid
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccesBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

$(function() {

	changeCommunity();

	// 初始化放大器
	$(function() {
		new TextMagnifier({
			inputElem : '#pCardNum',
			align : 'right',
			splitType : [ 6, 4, 4, 4 ]
		});
	});

	var selectHids = $("#ul_selected li");
	if (selectHids) {
		$.each(selectHids, function(index) {
			var hid = $(this).attr("id");
			$("#txzc option[value='" + hid + "']").remove();
		});
	}

	$("#cpSelect2").select2({
		placeholder : "请选择物业公司"
	});

	$("#cSelect2").select2({
		placeholder : "请选择小区"
	});

	$("#txzc").val('').select2({
		multiple : true,
		placeholder : txzcSize == 0 ? "该小区尚未录入房屋信息" : "请选择待选资产",
		width : '100%'
	});

	$("#cpSelect2").change(

	function() {
		$.ajax({
			url : "/IMS/owner/serchComm.ht",
			data : {
				pcid : $("#cpSelect2").val()
			},
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccesBar();
			},
			success : function(data) {
				if (data.code == 0) {
					$("#cSelect2").children().remove();
					if (data.result && data.result.length > 0) {
						$.each(data.result, function(index) {
							var dom = "<option value='" + this.cid + "' >" + this.communityName + "</option>";
							$("#cSelect2").append(dom);
						});
						$("#cSelect2").select2({
							placeholder : "请选择小区"
						});
					} else {
						$("#cSelect2").select2({
							placeholder : "该公司尚未录入小区",
							enable : false
						});
					}
					changeCommunity();
				}
				hideProccessBar();
			}
		});
	});

	$("#cSelect2").change(function() {
		changeCommunity();
	});

	$("#cpSelect2").select2({
		placeholder : "请选择物业公司",
		language : "zh-CN",
		ajax : {
			url : "/IMS/select/getCompanyList.ht",
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,
					page : param.page || 1
				};
			},
			processResults : function(data, param) {
				param.page = data.nextPage;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo,
		templateSelection : formatRepoSelection
	});
	$("#select2-cpSelect2-container").html(companyName);
});

function changeCommunity() {
	$.ajax({
		url : "/IMS/owner/serchHouse.ht",
		type : 'POST',
		data : {
			cid : $("#commId").val()
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccesBar();
		},
		success : function(data) {
			if (data.code == 0) {
				$("#txzc").children().remove();
				if (data.result && data.result.length > 0) {
					$.each(data.result, function(index) {
						var selected = $("#ul_selected li");
						var hid = this.hid;
						var isSelected = false;
						if (selected) {
							$.each(selected, function() {
								if (this.id == hid) {
									isSelected = true;
									return;
								}
							});
						}
						if (!isSelected) {
							var dom = "<option value='" + this.hid + "' >";
							dom += this.belongBuildingArea + this.belongBuildingNum + (this.belongBuildingUnit && this.belongBuildingUnit.length > 0 ? this.belongBuildingUnit + '单元' : '') + (this.comment && this.comment.length > 0 ? this.comment : '') + (this.houseNum && this.houseNum.length > 0 ? this.houseNum : '');
							dom += "</option>";
							$("#txzc").append(dom);
						}
					});
					$("#txzc").select2({
						placeholder : "请选择房产"
					});
				} else {
					$("#txzc").select2({
						placeholder : "该小区尚未录入房屋信息",
						enable : false
					});
				}
			}
			hideProccessBar();
		}
	});
}

function selected() {
	var hids = $("#txzc").val();
	var companyName = $("#cpSelect2 option:checked").text() || $("#companyName").val();
	var communityName = $("#cSelect2 option:checked").text() || $("#companyName").val();
	$.each(hids, function(index) { /* <![CDATA[ */
		var dom = "<li style='border-bottom: 1px solid #ccc;padding-bottom:3px;' id='" + this + "'>";
		dom += "<div>";
		dom += "<span style='width: 21%; float: left; display : none ;' id='c" + this + "'>" + (communityName || '&nbsp;') + "</span>";
		dom += "<span style='width: 50%; float: left; text-align : left; padding-left : 6px;' id='t" + this + "'>" + $("#txzc option[value='" + this + "']").text() + "</span> &nbsp;&nbsp;&nbsp;";
		dom += "<span style='width: 21%; float: left;'><input type='button' class='button-small' value='删除' onclick='" + "unSelected(\"" + this + "\")' /></span>"
		dom += "</div>";
		dom += "</li>";

		$("#txzc option[value='" + this + "']").remove();
		$("#ul_selected").append(dom);
		$("#div_hids").append("<input type='hidden' value='" + this + "' name='selectHids' />"); /* ]]> */
	});
	$("#txzc").parent().find("li.select2-selection__choice").remove();
}

function unSelected(hid) {
	var cpName = $("#cp" + hid).text();
	var cName = $("#c" + hid).text();

	var companyName = $("#cpSelect2 option:checked").text();
	var communityName = $("#cSelect2 option:checked").text(); /* <![CDATA[ */
	if (cpName == companyName && cName == communityName) {
		var dom = "<option value='" + hid + "'>" + $("#t" + hid).text() + "</option>";
		$("#txzc").append(dom); /* ]]> */
	}
	$("#" + hid).remove();
	$("#div_hids input[name=selectHids][value='" + hid + "']").remove();
}

var picIndex = 1;
function addPicLine() {
	var picTpl = $.format($("#copyTable").val());
	var index = picIndex;
	var optionDom = buildFamilySex('');
	var optionDomrelate = buildFamilyGx('');
	$('#copyBefore').before(picTpl(picIndex++));
	$("#fSex" + index).append(optionDom);
	$("#fRelate" + index).append(optionDomrelate);
	$("*[name='fRelate']").select2({
		placeholder : "请选择"
	});
	$("*[name='fFamilySex']").select2({
		placeholder : "请选择"
	});
}